<template>
  <div class="login">
    <common-head title="登录" />
    <div class="img">
      <img src="@/assets/imgs/m_hualogo.png" alt="" />
    </div>
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="userInfo.userName"
          name="userName"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="userInfo.pwd"
          type="password"
          name="pwd"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Form, Field } from 'vant'
import CommonHead from '@components/CommonHead'

Vue.use(Form).use(Field)
export default {
  data () {
    return {
      userInfo: {
        usreName: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      // params是输入框输入的内容，是通过v-model双向绑定的，可以通过params获取(vant)
      // 利用定时器模拟接口
      console.log(params)
      setTimeout(() => {
        const token = 'hahishuhdiv'
        const userInfo = {
          userName: params.userName,
          pwd: params.pwd
        }
        localStorage.setItem('token', token)
        // 存储的时候不应该直接存储字符串，而应该存储对象
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        // 点击提交之后，如果路由由地址的话，就跳转到原先的地址上，如果没有，就默认跳转到home页
        if (this.$route.query.from) {
          this.$router.replace(this.$route.query.from)
        } else {
          this.$router.replace('/home')
        }
      }, 1000)
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.login{
    background: #fff;
    height: 667px;
    /deep/.van-button--round{
        background: #ff734c;
        outline: none;
        border:none;
    }
    .img {
        display: flex;
        justify-content: center;
        img {
            padding-top: 70px;
            width: 200px;
            text-align: center;
        }
    }
    .van-form {
        padding-top: 35px;
    }
}
</style>
